// Copyright  2012 Petr TALLA. [petr.talla@gmail.com]
//
// This file is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This file is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY;  without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Lesser General Public License for more details.
// [http://www.gnu.org/copyleft/lesser.html]
//=============================================================================
#include "./TcObserver.h"
#include <TcObject.h>
#include <assert.h>
#include <stddef.h>

using namespace std;

//=============================================================================
TcObserver::TcObserver(TcObject* object)
{   objects_.push_back(object);
    object->observers_.push_back(this);
}

//=============================================================================
TcObject* TcObserver::get(int index)
{   if ( objects_.size() == 0 ) { assert(0); return NULL; }
    if ( index == 0 ) return objects_.front();

    if ( ( index < 0 ) || ( index >= (int)objects_.size() ) )
    {   assert(0);
        return NULL;
    }

    list<TcObject*>::iterator it = objects_.begin();
    while(true)
    {   assert(it != objects_.end());
        if ( index-- == 0 ) return *it;
        it++;
    }

    assert(0);
    return NULL;
}

//=============================================================================
